package collector.app.nexd.com.singlecollection.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import collector.app.nexd.com.singlecollection.R;
import collector.app.nexd.com.singlecollection.presenter.BasePresenter;
import collector.app.nexd.com.singlecollection.presenter.signin.SignInContract;
import collector.app.nexd.com.singlecollection.presenter.splash.LoadDataContract;
import collector.app.nexd.com.singlecollection.presenter.splash.LoadDataPresenter;
import collector.app.nexd.com.singlecollection.util.AnimUtil;
import collector.app.nexd.com.singlecollection.util.Logger;
import collector.app.nexd.com.singlecollection.util.ScreenUtils;

public class SplashActivity extends AppCompatActivity implements Animator.AnimatorListener, LoadDataContract.View {
    private ImageView ivHeader;
    private ObjectAnimator splashImgAnim;
    private ObjectAnimator splashBtnAnim;
    private Button btnLogin;
    private RelativeLayout mainContainer;
    private int finalYOffset;
    private LoadDataContract.Presenter loadDataPresenter;
    private static final String TAG = "SplashActivity";

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(R.layout.activity_splash);
        loadDataPresenter = new LoadDataPresenter(this, this);
        initView();
        init();
    }


    @Override
    public void initView() {
        ivHeader = (ImageView) findViewById(R.id.splash_img_head);
        btnLogin = (Button) findViewById(R.id.splash_login);
        mainContainer = (RelativeLayout) findViewById(R.id.rl_container);
    }


    @Override
    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(SplashActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void startActivity() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //切换界面动画，头像作为共享元素
            ActivityOptions options =
                    ActivityOptions.makeSceneTransitionAnimation(SplashActivity.this, ivHeader, ivHeader.getTransitionName());
            startActivity(new Intent(this, SignInActivity.class), options.toBundle());
            finish();
        } else {
            startActivity(new Intent(this, SignInActivity.class));
        }
    }

    @Override
    public void printLog(String msg) {
        Logger.DEBUG(TAG, msg);
    }


    protected void init() {
        finalYOffset = ScreenUtils.getScreenHeight(this);
        splashImgAnim = AnimUtil.splashAnim(ivHeader, 1.0f, -finalYOffset / 3);
        splashBtnAnim = AnimUtil.splashAnim(btnLogin, 1.0f, (float) (-finalYOffset / 3.5));
        splashBtnAnim.setStartDelay(500);
        splashBtnAnim.start();
        splashImgAnim.start();
        splashBtnAnim.addListener(this);
    }


    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        btnLogin.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity();
            }
        }, 1000);
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

}
